﻿using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;
using PDBSmartMVVM.model;

namespace PDBSmartMVVM.viewmodel
{
    public class ModelTreeViewModel : TreeViewItemViewModel
    {
        public ModelTreeViewModel(ModelTreeViewModel parent, Model model, ObservableCollection<object> objects)
            : base(parent, false)
        {
            Model = model;
            Objects = objects;
            CheckedAminos = new Dictionary<int, LinesVisual3D>();
            foreach (var amino in model.Aminos)
            {
                Children.Add(new AminoTreeViewModel(this, amino, objects));
            }
        }

        public Model Model { get; set; }
        public Dictionary<int, LinesVisual3D> CheckedAminos { get; set; }
        public ObservableCollection<object> Objects { get; set; }

        public void UnCheckAmino(int number)
        {
            if (CheckedAminos.ContainsKey(number - 1))
            {
                Objects.Remove(CheckedAminos[number - 1]);

            }
            if (CheckedAminos.ContainsKey(number))
            {
                Objects.Remove(CheckedAminos[number]);
            }

            CheckedAminos.Remove(number);
        }

        public void CheckAmino(int number)
        {
            LinesVisual3D next, prev;
            Point3D cpoint, npoint;
            if (!CheckedAminos.ContainsKey(number))
                CheckedAminos.Add(number, null);
            if (number > 0)
            {
                cpoint = ((Atom)Model.Aminos[number - 1].Atoms[AtomType.C]).Point;
                npoint = ((Atom)Model.Aminos[number].Atoms[AtomType.N]).Point;
                prev = new LinesVisual3D { Color = Colors.Gold, Points = new List<Point3D>() { cpoint, npoint } };
                if (CheckedAminos.ContainsKey(number - 1))
                {
                    Objects.Remove(CheckedAminos[number - 1]);
                    Objects.Add(prev);
                    CheckedAminos[number - 1] = prev;
                }
            }

            if (number < Model.Aminos.Count - 1)
            {
                cpoint = ((Atom)Model.Aminos[number].Atoms[AtomType.C]).Point;
                npoint = ((Atom)Model.Aminos[number + 1].Atoms[AtomType.N]).Point;
                next = new LinesVisual3D { Color = Colors.Gold, Points = new List<Point3D>() { cpoint, npoint } };
                if (CheckedAminos.ContainsKey(number + 1))
                {
                    Objects.Remove(CheckedAminos[number]);
                    Objects.Add(next);
                    CheckedAminos[number] = next;
                }
            }
        }

        private RelayCommand _clearModelCommand;
        public ICommand ClearModelCommand
        {

            get { return _clearModelCommand ?? (_clearModelCommand = new RelayCommand(ClearModelCommandExecute)); }
        }

        private void ClearModelCommandExecute(object o)
        {
            this.IsCalculated = false;
        }

        private RelayCommand _selectModelCommand;
        public ICommand SelectModelCommand
        {

            get { return _selectModelCommand ?? (_selectModelCommand = new RelayCommand(SelectModelCommandExecute)); }
        }

        private void SelectModelCommandExecute(object o)
        {
            this.IsCalculated = true;
        }
    }
}

